var $window = $(window);

var winW = $window.width(),
    winH = $window.height();
var bgAudio = document.getElementById('bg-audio');
var longPull = {
    startY : 0,
    endY : 0,
    scrollBorder : '',
    minDistance : 20,
    topbar : $('#topbar').on('click', '#menu', function(event) {
            event.preventDefault();
            $('#topbar').removeClass('show');
            longPull.ft_page.removeClass('hide');
            $('.chapter-box').addClass('hide');
        }),
    menu : $('#menu'),
    nav : $('.nav'),
    ft_page : $('.ft-page'),
    chapter_box : $('.chapter-box').on('touchstart',function(event){
            //event.preventDefault();
            longPull.menu.removeClass('menu-active');
            longPull.nav.stop().slideUp('slow');
    }),
    cur_chapter:'',
    slides : $('.ft-page-slider').width("100%").height(winH),
    ajax_loading : $('.ajax-loading'),
    
    chapter_li : $('.chapter-li').on('click',function(){
            var $this = $(this);
            $this.siblings().removeClass('active');
            $this.addClass('active');
            $('.chapter-box').removeClass('hide');
            setTimeout(function() {
                longPull.loadChapter($this.data('chapter'));
                //!longPull.topbar.hasClass('show')?longPull.topbar.addClass('show'):'';
            }, 500);
        }),
    nav_li:$('.nav-li').on('touchend',function(){
                    $(this).removeClass('show');
                    longPull.loadChapter(this.dataset.chapter);
                    //!longPull.topbar.hasClass('show')?longPull.topbar.addClass('show'):'';
                    longPull.menu.toggleClass('menu-active');
                    longPull.nav.stop().slideToggle();
                    setTimeout(function() {
                        longPull.loadChapter($(this).data('chapter'));
                        !longPull.topbar.hasClass('show')?longPull.topbar.addClass('show'):'';
                    }, 1000);
                })
                .on('touchstart',function(){
                    $(this).addClass('show');
                }),
    chapterTouchStarted:function(event){
            longPull.scrollBorder = longPull.scrollEnd(this);

            if(event.originalEvent.targetTouches){
                longPull.startY = event.originalEvent.targetTouches[0].clientY;
            }else{
                longPull.startY = event.clientY;
            }
            if((longPull.scrollBorder=='scrollTopEnd')||(longPull.scrollBorder=='scrollBottomEnd')){
                    longPull.ajax_loading.addClass('show');
                }
        },
    chapterTouchMoved:function(event){
            if(longPull.startY>0){
                if(event.originalEvent.targetTouches){
                    longPull.endY = event.originalEvent.changedTouches[0].clientY;
                }else{
                    longPull.endY = event.clientY;
                }
            }
    },
    chapterTouchEnded:function(event){
        if(longPull.startY>0){
                if(event.originalEvent.targetTouches){
                    longPull.endY = event.originalEvent.changedTouches[0].clientY;
                }else{
                    longPull.endY = event.clientY;
                }
            }
            longPull.ajax_loading.removeClass('show');
            var  yDis = longPull.endY - longPull.startY;
            if(Math.abs(yDis)>longPull.minDistance&&longPull.endY>0){
                if(yDis<0&&longPull.scrollBorder=='scrollBottomEnd'){
                    //swipe up and load next chapter;
                    var nextChapter = this.dataset.next;
                    longPull.loadChapter(nextChapter);
                }
                else if(yDis>0&&longPull.scrollBorder=='scrollTopEnd'){
                    //swipe down and load prev chapter;
                    var prevChapter = this.dataset.prev;
                    if(longPull.cur_chapter=='boarding'){
                        longPull.backToMenu();
                    }
                    else{
                        longPull.loadChapter(prevChapter);
                    }
                }
            }
            longPull.startY = longPull.endY=0;
        },
    scrollEnd:function(chapter){
            if(scrollY==0){
                return 'scrollTopEnd';
            }
            if((scrollY+winH)==chapter.scrollHeight){
                return 'scrollBottomEnd';
            }
            return false;
        },
    backToMenu:function(){
            //longPull.ft_page_swiper.slideTo(0);
            longPull.ft_page.removeClass('hide');
            longPull.chapter_box.addClass('hide');
            longPull.topbar.removeClass('show');
            longPull.cur_chapter='';
            longPull.chapter_li.removeClass('active');
    },
    loadChapter:function(chapter){
            if(chapter =='qh'){
                bgAudio.play();
                bgAudio.volume = .4;
            }
            else{
                bgAudio.pause();
            }
            if(!chapter||(longPull.cur_chapter == chapter)) {
                longPull.ft_page.addClass('hide');
                return;
            };
            longPull.cur_chapter = chapter;

            longPull.nav_li.removeClass('active')
                    .filter(function(){
                        return this.dataset.chapter==chapter;
                    })
                    .addClass('active');
            $.ajax({
                type:'GET',
                url:chapter+'.html',
                dataType:'html',
                xhr: function() {
                    var xhr = new window.XMLHttpRequest();
                    xhr.addEventListener('progress', function(e) {
                        if (e.lengthComputable) {
                            longPull.ajax_loading.addClass('show');
                        }
                    });
                    return xhr;
                    },
                success:function(data,textStatus){
                    longPull.ft_page.addClass('hide');
                    longPull.ajax_loading.removeClass('show');
                    longPull.chapter_box.html(data)
                            .removeClass('hide');

                    $('.chapter').on('touchstart',longPull.chapterTouchStarted)
                                .on('touchend',longPull.chapterTouchEnded);
                    window.scrollTo(0,0);
                    longPull.loadingEnd();
                    },
                beforeSend:function(){
                    longPull.ajax_loading.addClass('show');
                }
            });
        },
    loadingEnd:function(){
        var cover = document.getElementById("cover");
        cover.addEventListener('touchstart',function(ev){
            oldPos = ev.changedTouches[0]['clientY'];
        });
        $('.full-height').css({'width':"100%",'height':winH});
        $('.footer-nav-li').on('click',function(){
                longPull.loadChapter(this.dataset.chapter);
            });    
    }
};

$(function(){
    var coverSteps = $('.cover-steps-animation').css({
            // 'transform':'scale('+winW/640+')'
        });
    var image = new Image();
    image.onload = function(){
        $('.ft-page-movie-box').addClass('show');

        coverSteps.css('background-image','url('+image.src+')');

        //init ft-page slide;
        longPull.ft_page_swiper = new Swiper('.ft-page',{
                slideClass:'ft-page-slider',
                wrapperClass:'ft-page-wrapper',
                direction:'vertical'
            })
    }
    image.src = '../views/8Version1/images/cover-bg.gif';

    $("#menu").on("click", function(){
        location.href="http://app.dongxihu.gov.cn/8Version1/index.html";
    });
})
/*
//wxshare config
var directory = location.origin+location.pathname.substring(0, location.pathname.lastIndexOf('/'));//  文档目录
var shareInfo = {
    title:$('title').text(),    //填写社交分享的标题；
    desc:"砥砺奋进的国家网安基地 长江日报临空港编辑部 以价值创造价值。", 
    link:location.href,    //填写社交分享的链接；
    img:"http://app.dongxihu.gov.cn/images/logo.png",//分享的图片地址
};
*/